package com.grt192.sensor;

import java.io.IOException;

import com.grt192.core.Sensor;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.IAccelerometer3D;

/**
 * A DemoJoystick takes advantage of the sensorboard accelerometer to provide a
 * convenient joystick
 * 
 * @author Andrew Chen <andrewtheannihilator@gmail.com>
 */
public class DemoJoystick extends Sensor {

	/** Key for accessing X tilt */
	public static final String X = "X";
	/** Key for accessing Y tilt */
	public static final String Y = "Y";
	/** Key for accessing Z tilt */
	public static final String Z = "Z";
	// sensor
	private IAccelerometer3D acc;

	public DemoJoystick(int pollTime, String id) {
		acc = EDemoBoard.getInstance().getAccelerometer();
		setSleepTime(pollTime);
		setID(id);
	}

	/**
	 * Gets accelerometer tilt on x axis
	 * 
	 * @return
	 */
	public double getTiltX() {
		try {
			return acc.getTiltX();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return Sensor.ERROR;

	}

	/**
	 * Gets accelerometer tilt on y axis
	 * 
	 * @return
	 */
	public double getTiltY() {
		try {
			return acc.getTiltY();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return Sensor.ERROR;

	}

	/**
	 * Gets accelerometer tilt on z axis
	 * 
	 * @return
	 */
	public double getTiltZ() {
		try {
			return acc.getTiltZ();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return Sensor.ERROR;
	}

	public void poll() {
		setState(X, getTiltX());
		setState(Y, getTiltY());
		setState(Z, getTiltZ());
	}
}
